package com.zrar.ak.gkpt.service.impl.common;

import com.zrar.ak.gkpt.dao.ICommonAnnexInfoDao;
import com.zrar.ak.gkpt.model.bo.CommonAnnexInfoBO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.common.ICommonAnnexInfoService;
import com.zrar.ak.gkpt.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: CommonAnnexInfoService
 * @Description 附件上传和下载操作Service层
 * @Author liyang
 * @Time 2020/04/03
 */
@Service
public class CommonAnnexInfoService implements ICommonAnnexInfoService {

    @Value("${annex.pathUrl}")
    private String pathUrl;

    @Autowired
    private ICommonAnnexInfoDao commonAnnexInfoDao;


    @Override
    public JsonResult annexUpload(JsonResult jsonResult, MultipartFile file, String annexStatus, UserVO userVO) {
        try {
            String annexFullName = file.getOriginalFilename();
            String annexName = annexFullName.substring(0,annexFullName.lastIndexOf("."));
            String fileType = annexFullName.substring(annexFullName.lastIndexOf(".")+1,annexFullName.length());
            String path = pathUrl;
            path = path.replace("\\", "/");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

            if (path.lastIndexOf('/') == path.length() - 1) {
                path += (sdf.format(new Date()) + "/" + System.currentTimeMillis());
            } else {
                path += ("/" + sdf.format(new Date()) +  "/" + System.currentTimeMillis());
            }
            InputStream in = file.getInputStream();
            FileUtil.uploadFile(in, path, annexFullName);

            String creatorId = userVO.getUserId();
            Date createTime = new Date();
            String updateUserId = userVO.getUserName();
            Date updateTime = new Date();
            CommonAnnexInfoBO commonAnnexInfoBO = new CommonAnnexInfoBO();
            commonAnnexInfoBO.setId(CommonTools.getUUID());
            commonAnnexInfoBO.setCreatorId(creatorId);
            commonAnnexInfoBO.setCreateTime(createTime);
            commonAnnexInfoBO.setUpdateUserId(updateUserId);
            commonAnnexInfoBO.setUpdateTime(updateTime);
            commonAnnexInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            commonAnnexInfoBO.setAnnexName(annexName);
            commonAnnexInfoBO.setAnnexFullName(annexFullName);
            commonAnnexInfoBO.setSavePath(path + "/" + annexFullName);
            commonAnnexInfoBO.setAnnexStatus(annexStatus);
            commonAnnexInfoBO.setFileType(fileType);
            commonAnnexInfoDao.insert(commonAnnexInfoBO);

            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            Map<String,String> jsonData = new HashMap<>();
            jsonData.put("id", commonAnnexInfoBO.getId());
            jsonData.put("annexFullName", commonAnnexInfoBO.getAnnexFullName());
            jsonResult.setData(jsonData);
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public CommonAnnexInfoBO getAnnexInfoById(String id) {
        CommonAnnexInfoBO commonAnnexInfoBO = commonAnnexInfoDao.selectByPrimaryKey(id);
        return commonAnnexInfoBO;
    }

    @Override
    @Transactional
    public JsonResult annexDelete(JsonResult jsonResult, String id, UserVO userVO) {
        CommonAnnexInfoBO commonAnnexInfoBO = commonAnnexInfoDao.selectByPrimaryKey(id);
        commonAnnexInfoBO.setIsDelete(BusinessEnum.IS_DELETE_Y.getValue());
        commonAnnexInfoDao.updateByPrimaryKey(commonAnnexInfoBO);
        File file = new File(commonAnnexInfoBO.getSavePath());
        boolean bool = FileUtil.deleteDir(file);
        if(bool){
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        }else{
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }






}
